﻿#include "stock.h"
#include "ui_stock.h"

Stock::Stock(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Stock)
{
    ui->setupUi(this);
}

Stock::~Stock()
{
    delete ui;
}

bool Stock::init()
{
    if(option.load(LST_PROVIDER))
    {
        ui->provider->addItems(option.getStringList());
        ui->provider->addItem(OPT_EDIT);
        ui->provider->setCurrentIndex(-1);

        model=new QSqlTableModel(this);
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        ui->tableView->setModel(model);

        sender=new QUdpSocket(this);
        return true;
    }
    else
        return false;
}

void Stock::reload(const OptionType &type)
{
    if(type == O_PROVIDER)
    {
        if(option.load(LST_PROVIDER))
        {
            ui->provider->clear();
            ui->provider->addItems(option.getStringList());
            ui->provider->addItem(OPT_EDIT);
        }
    }
}

void Stock::updateView()
{
    model->setTable(TB_STOCK);
    model->setFilter(QString("date = '%1'").arg(Env::date));
    model->select();

    model->setHeaderData(0,Qt::Horizontal,QStringLiteral("ID"));
    model->setHeaderData(1,Qt::Horizontal,QStringLiteral("日期"));
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("名称"));
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("数量"));
    model->setHeaderData(4,Qt::Horizontal,QStringLiteral("来源"));
    model->setHeaderData(5,Qt::Horizontal,QStringLiteral("备注"));
    model->setHeaderData(6,Qt::Horizontal,QStringLiteral("是否完成？"));
}

void Stock::sendMsg(const QString &title, const QString &content)
{
    QByteArray data;
    QDataStream out(&data,QIODevice::WriteOnly);
    out << title << content ;
    sender->writeDatagram(data,data.length(),QHostAddress("192.168.1.255"),SEND_PORT);

    ui->lbl_status->setText(QStringLiteral("通知已发送"));
    ui->notice->clear();
}

bool Stock::saveRecord()
{
    QSqlQuery query;
    query.prepare("insert into td_stock (date,name,number,provider,remark) values (?,?,?,?,?)");
    query.addBindValue(Env::date);
    query.addBindValue(name);
    query.addBindValue(number);
    query.addBindValue(provider);
    query.addBindValue(remark);
    return query.exec();
}

void Stock::on_btn_send_clicked()
{
    notice=ui->notice->toPlainText();
    if(notice.isEmpty())
        notice=QStringLiteral("仓库有新的入库单，请注意查看。");

    sendMsg(T_STOCK ,notice);
}

void Stock::on_btn_submit_clicked()
{
    name=ui->name->text().trimmed();
    provider=ui->provider->currentText();
    if(name.isEmpty() || provider.isEmpty() || ui->number->text().isEmpty())
    {
        QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("名称、数量、供货商不能为空！"),QMessageBox::Ok);
    }
    else
    {
        number=ui->number->text();
        remark=ui->remark->text();
        if(saveRecord())
        {
            updateView();
            ui->name->clear();
            ui->number->clear();
            ui->provider->setCurrentIndex(-1);
            ui->remark->clear();
            ui->lbl_status->setText(QStringLiteral("记录已提交"));
        }
        else
            ui->lbl_status->setText(QStringLiteral("提交失败！数据库异常"));
    }
}

void Stock::on_btn_save_clicked()
{
    model->submitAll();
}

void Stock::on_provider_currentTextChanged(const QString &text)
{
    if(text == OPT_EDIT)
    {
        OptionEditor editor;
        if(editor.detect(O_PROVIDER))
        {
            connect(&editor,SIGNAL(reload(OptionType)),this,SLOT(reload(OptionType)));
            if(editor.exec() == QDialog::Rejected)      //手动关闭，值为0
                ui->provider->setCurrentIndex(-1);
        }
        else
            QMessageBox::critical(this,QStringLiteral("错误"),QStringLiteral("无法加载配置文件！"),QMessageBox::Ok);
    }
}
